from django.db import models
from django.contrib.auth.models import User

class HegelianTriangulation(models.Model):
    user = models.ForeignKey(User)
    date = models.DateTimeField(auto_now_add = True)
    subject = models.CharField(max_length = 500)
    
    thesis = models.TextField(null = True, default = None)
    thesis_result = models.OneToOneField("TriangulateResult", null = True, default = None, related_name = 'thesis_result_of')
    antithesis = models.TextField(null = True, default = None)
    antithesis_result = models.OneToOneField("TriangulateResult", null = True, default = None, related_name = 'antithesis_result_of')
    synthesis = models.TextField(null = True, default = None)
    
    def __unicode__(self):
        
        r1 = None
        r2 = None
        if self.thesis_result:
            r1 = self.thesis_result.reliability
        if self.antithesis_result:
            r2 = self.antithesis_result.reliability
        
        print r1
        print r2
        
        res = ''
        if r1 and r2:
            res = "%s - Complete triangulation with %d%% vs. %d%%" % (self.date.strftime('%d %b %Y %H:%M'), r1, r2)
        elif r1:
            res = "%s - Triangulation of %d%% (only thesis)" % (self.date.strftime('%d %b %Y %H:%M'), r1)
        else:
            res = "%s - Incomplete triangulation" % (self.date.strftime('%d %b %Y %H:%M'))
        
        return res

class TriangulateResult(models.Model):
    link = models.ForeignKey("Link")
    reliability = models.PositiveSmallIntegerField(null = True, default = None)
    
    data_result = models.ForeignKey("DataTriangulateResult", null = True, default = None)
    investigator_result = models.ForeignKey("InvestigatorTriangulateResult", null = True, default = None)
    theory_result = models.ForeignKey("TheoryTriangulateResult", null = True, default = None)
    method_result = models.ForeignKey("MethodTriangulateResult", null = True, default = None)

class DataTriangulateResult(models.Model):
    links = models.ManyToManyField("Link")
    reliability = models.PositiveSmallIntegerField(null = True, default = None)
    notes = models.TextField()

class InvestigatorTriangulateResult(models.Model):
    links = models.ManyToManyField("Link")
    whois = models.TextField()
    additional_info = models.TextField()
    reliability = models.PositiveSmallIntegerField(null = True, default = None)
    notes = models.TextField()

class TheoryTriangulateResult(models.Model):
    reliability = models.PositiveSmallIntegerField(null = True, default = None)
    notes = models.TextField()

class MethodTriangulateResult(models.Model):
    methods = models.ManyToManyField("Method")
    reliability = models.PositiveSmallIntegerField(null = True, default = None)
    notes = models.TextField()

class Link(models.Model):
    link = models.TextField()

class Tag(models.Model):
    tag = models.CharField(max_length=100)
    count = models.PositiveIntegerField()
    theory_result = models.ForeignKey("TheoryTriangulateResult")

class Method(models.Model):
    method = models.CharField(max_length = 100, unique = True)

class HelpDocument(models.Model):
    name = models.CharField(max_length = 100, unique = True)
    text = models.TextField()